<?

/**
* local collection functions of tocmenu_Y2
*
* @author Adesanto Asman
* @version 1.0
* @license Free
* @copyright Copyright (c)2007, Adesanto Asman
* @package CLFetchers
* 
*/

/**
* @package CLFetchers
* 
*/
class CLFetchers{
  
  /**
  * fetch the list of valid transactions
  *
  * @param  string    $Db
  * @param  string    $condt      the condition
  * @param  string    $srcDb
  * @param  string    $lstResult
  *
  */
  function fetchLstTransaction(&$Db,
                               $condt,
        	                     &$srcDbHdr,
        	                     &$srcDbPos,
        	                     &$srcDbDept,
        	                     &$nResult,
        	                     &$lstResult){
        	                      
    $query  = "SELECT ".$srcDbPos.".POS,
                      ".$srcDbDept.".DEPT,
                      ".$srcDbHdr.".GROUP_ID
               
               FROM ".$srcDbHdr.",
                    ".$srcDbPos.",
                    ".$srcDbDept."
               
               WHERE  ".$srcDbHdr.".STATUS=1
                      
                      AND ".$srcDbPos.".POS_ID=".$srcDbHdr.".POS_ID
                      
                      AND ".$srcDbDept.".DEPT_ID=".$srcDbHdr.".DEPT_ID
               
               ORDER BY ".$srcDbHdr.".GROUP_ID ASC ";
               
    $Db->execFetchQuery($query,
                        $nResult,
                        $lstResult);
  }
  
  /**
  * fetch the header detail
  * 
  * @param  string  $Db
  * @param  integer $groupId
  * @param  string  $srcDbHdr
  * @param  string  $srcDbPos
  * @param  string  $srcDbDept
  * @param  integer $nHdr
  * @param  array   $resHdr
  *
  */
  function fetchUsrHdr(&$Db,
                       &$groupId,
                       &$srcDbHdr,
                       &$srcDbPos,
                       &$srcDbDept,
                       &$nHdr,
                       &$resHdr){
                        
    $query  = "SELECT ".$srcDbPos.".POS,
                      ".$srcDbDept.".DEPT
                      
               FROM ".$srcDbHdr.",
                    ".$srcDbPos.",
                    ".$srcDbDept."
                    
               WHERE  ".$srcDbHdr.".GROUP_ID=".$groupId."
                
                      AND ".$srcDbPos.".POS_ID=".$srcDbHdr.".POS_ID
                      
                      AND ".$srcDbDept.".DEPT_ID=".$srcDbHdr.".DEPT_ID ";
                      
    $Db->execFetchQuery($query,
                        $nHdr,
                        $resHdr);
  }
  
  /**
  * fetch userid detail 
  * 
  * @param  string  $Db
  * @param  integer $groupId
  * @param  string  $srcDb
  * @param  integer $nDtl
  * @param  array   $resDtl
  *
  */
  function fetchUsrDtl(&$Db,
                       &$groupId,
                       &$srcDb,
                       &$nDtl,
                       &$resDtl){
                        
    $query  = "SELECT *
               FROM ".$srcDb."
               WHERE  GROUP_ID=".$groupId."
               ORDER BY NO ASC ";
                      
    $Db->execFetchQuery($query,
                        $nDtl,
                        $resDtl);
  }
  
  /**
  * fetch access list
  * 
  * @param  string    $Db
  * @param  string    $srcDb
  * @param  string    $nAccess
  * @param  string    $lstAccess
  *
  */
  function fetchLstAccess(&$Db,
                          &$srcDb,
                          &$nAccess,
                          &$lstAccess){
    
    $query  = "SELECT TRANSACTION,
                      LEVELMN
               FROM ".$srcDb."
               WHERE  STATUS='"._ACTIVE."' 
               ORDER BY LEVELMN ASC";
               
    $Db->execFetchQuery($query,
                        $nAccess,
                        $lstAccess);
                        
  }
  
  /**
  * fetch posid
  * 
  * @param  string  $Db
  * @param  string  $usrPos
  * @param  string  $srcDb
  * @param  string  $posId
  *
  */
  function fetchPosId(&$Db,
                      &$usrPos,
                      &$srcDb,
                      &$posId){
                                             
    $query  = "SELECT POS_ID
               FROM ".$srcDb."
               WHERE  POS='".$usrPos."' ";
               
    $Db->execFetchQuery($query,
                        $nrows,
                        $result);
                        
    $posId = $result["POS_ID"][0];
  }
  
  /**
  * fetch deptId
  * 
  * @param  string  $Db
  * @param  string  $usrDept
  * @param  string  $srcDb
  * @param  string  $deptId
  *
  */
  function fetchDeptId(&$Db,
                       &$usrDept,
                       &$srcDb,
                       &$deptId){
                                             
    $query  = "SELECT DEPT_ID
               FROM ".$srcDb."
               WHERE  DEPT='".$usrDept."' ";
               
    $Db->execFetchQuery($query,
                        $nrows,
                        $result);
                        
    $deptId = $result["DEPT_ID"][0];
  }
  
  /**
  * fetch new group id
  *
  * @param  string  $Db
  * @param  integer $posId
  * @param  integer $deptId
  * @param  string  $srcDb
  * @param  boolean $isExists
  * @param  integer $groupId
  *
  */
  function fetchNewGroupId(&$Db,
                           &$posId,
                           &$deptId,
                           &$srcDb,
                           &$isExists,
                           &$groupId){
                            
    /**
    * check if this criteria/template is exists
    *
    */
    $this->isCriteriaExists($Db,
                            $posId,
                            $deptId,
                            $srcDb,
                            $isExists);
    
    if(!$isExists){
      
      $query = "SELECT GROUP_ID
                FROM ".$srcDb."
                ORDER BY GROUP_ID DESC ";
                
      $Db->execFetchQuery($query,
                          $nrows,
                          $result);
                          
      if($nrows > 0){
        
        $groupId = $result["GROUP_ID"][0] + 1;
      }else{
        
        $groupId = 1;
      }
    }else{
      
      /* do nothing */
    }
  }
  
  /**
  * validate no duplicate
  * 
  * @param  string  $Db
  * @param  integer $posId
  * @param  integer $deptId
  * @param  string  $srcDb
  * @param  boolean $isExists
  * 
  */
  function isCriteriaExists(&$Db,
                            &$posId,
                            &$deptId,
                            &$srcDb,
                            &$isExists){
                              
    $query  = "SELECT *
               FROM ".$srcDb."
               WHERE  POS_ID=".$posId."
                      AND DEPT_ID=".$deptId." ";
                      
    $Db->execFetchQuery($query,
                        $nrows,
                        $result);
    
    if($nrows > 0){
      
      $isExists = true;
    }else{
      
      $isExists = false;
    }
  }
  
  /**
  * fetch list of pos
  * 
  * @param  string    $Db
  * @param  string    $srcDb
  * @param  string    $nPos
  * @param  string    $lstPos
  *
  */
  function fetchLstPos(&$Db,
                       &$srcDb,
                       &$nPos,
                       &$lstPos){
                        
    $query  = "SELECT POS
               FROM ".$srcDb."
               WHERE  STATUS=1 
               ORDER BY POS_ID ASC";
               
    $Db->execFetchQuery($query,
                        $nPos,
                        $result);
                        
    $lstPos = $result["POS"];
  }
  
  /**
  * fetch list of department
  * 
  * @param  string    $Db
  * @param  string    $srcDb
  * @param  string    $nDept
  * @param  string    $lstDept
  *
  */
  function fetchLstDept(&$Db,
                        &$srcDb,
                        &$nDept,
                        &$lstDept){
                        
    $query  = "SELECT DEPT
               FROM ".$srcDb."
               WHERE  STATUS=1 
               ORDER BY DEPT_ID ASC";
               
    $Db->execFetchQuery($query,
                        $nDept,
                        $result);
                        
    $lstDept = $result["DEPT"];
  }
}

?> 